package com.lx.spring.anno;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class Demo {
    public static void main(String[] args) throws NoSuchMethodException,
            InvocationTargetException,
            IllegalAccessException {
        A a = new A();
        B b = new B();
        a.test();
        getObject(a);
        getObject(b);
    }

    public static void getObject(Object object) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<?> aClass = object.getClass();
        //包名加类名
        System.out.println("aClass.getName() = " + aClass.getName());
        System.out.println("aClass.getSimpleName() = " + aClass.getSimpleName());
        //公共的属性
        for (Field field : aClass.getFields()) {
            System.out.println("field = " + field);
        }
        //定义的属性
        for (Field declaredField : aClass.getDeclaredFields()) {
            System.out.println("declaredField = " + declaredField);
        }

        //获得 aClass这个类的声明方法
        Method test = aClass.getDeclaredMethod("test", int.class);
        if (test != null) {
            // 使私有方法可以调用
            test.setAccessible(true);
            test.invoke(object, 1);
        }

    }

}



class A {
    private String a;
    protected int b;
    public Date c;
    byte d;

    public void test(int num){
//        int i = 1/0;
        System.out.println("test_method_int_A");
    }
    private void test(String num){
        System.out.println("test_method_String");
    }
    public void test(){
        int i = 1/0;
    }

}

class B extends A{

}
